// common/time/choosetime.js
let common = require('../compents.js');
const moment = require('../../utils/moment.min.js');
moment.locale('zh-cn');
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  ready: function () {
    let _time = moment().week(moment().week() + 1).startOf('week')
      .format("YYYY-MM-DD");
  },
  /**
   * 组件的初始数据
   */
  data: {
    time: '第' + common.weekDate().weekth.slice(-2) + '周 ' + common.weekDate().start + '~' + common.weekDate().end,
    index: 1,
    title: "周数据",
    columns: ['日数据', '周数据', '月数据', '年数据'],
    show: false,
    currentDate: common.weekDate().start,
    isAdd:true,
    isSub:true,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event) {
      this.setData({
        index: event.detail.index,
        title: this.data.columns[event.detail.index]
      })
    },
    open() {
      this.triggerEvent('map', {
        visible: false
      })
      this.setData({
        show: true
      })
    },
    sure() {
      if (this.data.index == 0) {
        this.triggerEvent('change', {
          period: common.beforeDate().replace(/-/g, ''),
          date_type: 1,
          start_date: common.beforeDate().replace(/-/g, ''),
          time: common.beforeDate(),
        })
        this.setData({
          show: false,
          time: common.beforeDate(),
          currentDate: common.beforeDate(),
          isAdd: false,
        })
      } else if (this.data.index == 1) {
        this.triggerEvent('change', {
          period: common.weekDate().weekth,
          date_type: 2,
          start_date: common.weekDate().start.replace(/-/g, ''),
          time: common.weekDate().start + '~' + common.weekDate().end,
        })
        this.setData({
          show: false,
          time: '第' + common.weekDate().weekth.slice(-2) + '周 ' + common.weekDate().start + '~' + common.weekDate().end,
          currentDate: common.weekDate().start,
          isAdd: false,
        })
      } else if (this.data.index == 2) {
        this.triggerEvent('change', {
          period: common.getData(common.currenTime).replace(/-/g, '').slice(0, -2),
          date_type: 3,
          start_date: common.getDateByMonth(common.currenTime).startTime.replace(/-/g, ''),
          time: common.getDateByMonth(common.currenTime).startTime + '~' + common.getDateByMonth(common.currenTime).endTime,
        })
        this.setData({
          show: false,
          time: common.getDateByMonth(common.currenTime).startTime + '~' + common.getDateByMonth(common.currenTime).endTime,
          currentDate: common.getDateByMonth(common.currenTime).startTime,
          isAdd: false,
        })
      } else if (this.data.index == 3) {
        this.triggerEvent('change', {
          period: common.currenTime.getFullYear(),
          date_type: 4,
          start_date: common.currenTime.getFullYear() + '0101',
          time: common.currenTime.getFullYear() + '-01-01' + '~' + common.currenTime.getFullYear() + '-12-31',
        })
        this.setData({
          show: false,
          time: common.currenTime.getFullYear() + '-01-01' + '~' + common.currenTime.getFullYear() + '-12-31',
          currentDate: common.currenTime.getFullYear() + '-01-01',
          isAdd:false
        })
      }
      this.triggerEvent('map', {
        visible: true
      })
    },
    before() {
      let _period, date_type, start_date, _time,_current = this.data.currentDate;
      if (this.data.index == 0) {
        date_type = 1;
        _period = moment(_current).subtract(1, "days").format("YYYYMMDD");
        start_date = moment(_current).subtract(1, "days").format("YYYYMMDD");
        _time = moment(_current).subtract(1, "days").format("YYYY-MM-DD");
      } else if (this.data.index == 1) {
        date_type = 2;
        _period = common.getCurrenWeek(moment(_current).week(moment(_current).week() - 1).startOf('week').format("YYYY-MM-DD"));
        start_date = moment(_current).week(moment(_current).week() - 1).startOf('week').format("YYYYMMDD");
        _time = '第' + (moment(_current).week() - 1) + '周 ' + moment(_current).week(moment(_current).week() - 1).startOf('week').format("YYYY-MM-DD") + '~' + moment(_current).week(moment(_current).week() - 1).endOf('week').format("YYYY-MM-DD");
      } else if (this.data.index == 2) {
        date_type = 3;
        _period = moment(_current).month(moment(_current).month() - 1).startOf('month').format("YYYYMM");
        start_date = moment(_current).month(moment(_current).month() - 1).startOf('month').format("YYYYMMDD");
        _time = moment(_current).month(moment(_current).month() - 1).startOf('month').format("YYYY-MM-DD") + '~' + moment(_current).month(moment(_current).month() - 1).endOf('month').format("YYYY-MM-DD");
      } else if (this.data.index == 3) {
        date_type = 4;
        _period = moment(_current).year() - 1;
        start_date = (moment(_current).year() - 1) + '-01-01';
        _time = (moment(_current).year() - 1) + '-01-01' + '~' + (moment(_current).year() - 1) + '-12-31';
      }
      // console.log(77,_current,_period, date_type, start_date, _time);
      this.triggerEvent('change', {
        period: _period,
        date_type: date_type,
        start_date: start_date,
        time: _time,
      })
      this.setData({
        show: false,
        time: _time,
        currentDate: start_date,
        isAdd: true
      })
    },
    after() {
      let _period, date_type, start_date,end_date,_time,_current = this.data.currentDate,_isAdd;
      if (this.data.index == 0) {
        date_type = 1;
        _period = moment(_current).add(1, "days").format("YYYYMMDD");
        start_date = moment(_current).add(1, "days").format("YYYYMMDD");
        end_date = moment(_current).add(1, "days").format("YYYYMMDD");
        _time = moment(_current).add(1, "days").format("YYYY-MM-DD");
      } else if (this.data.index == 1) {
        date_type = 2;
        _period = common.getCurrenWeek(moment(_current).week(moment(_current).week() + 1).startOf('week').format("YYYY-MM-DD"));
        start_date = moment(_current).week(moment(_current).week() + 1).startOf('week').format("YYYYMMDD");
        end_date = moment(_current).week(moment(_current).week() + 1).endOf('week').format("YYYYMMDD");
        _time = '第' + (moment(_current).week() + 1) + '周 ' + moment(_current).week(moment(_current).week() + 1).startOf('week').format("YYYY-MM-DD") + '~' + moment(_current).week(moment(_current).week() + 1).endOf('week').format("YYYY-MM-DD");
      } else if (this.data.index == 2) {
        date_type = 3;
        _period = moment(_current).month(moment(_current).month() + 1).startOf('month').format("YYYYMM");
        start_date = moment(_current).month(moment(_current).month() + 1).startOf('month').format("YYYYMMDD");
        end_date = moment(_current).month(moment(_current).month() + 1).endOf('month').format("YYYYMMDD");
        _time = moment(_current).month(moment(_current).month() + 1).startOf('month').format("YYYY-MM-DD") + '~' + moment(_current).month(moment(_current).month() + 1).endOf('month').format("YYYY-MM-DD");
      } else if (this.data.index == 3) {
        date_type = 4;
        _period = moment(_current).year() + 1;
        start_date = (moment(_current).year() + 1) + '-01-01';
        end_date = (moment(_current).year() + 1) + '-12-31';
        _time = (moment(_current).year() + 1) + '-01-01' + '~' + (moment(_current).year() + 1) + '-12-31';
      }
      let _diff;
      if(this.data.index == 0){
        _diff = moment().subtract(1, "days").diff(moment(end_date), 'days')
      }else{
        _diff = moment().diff(moment(end_date), 'days')
      }
      if(_diff > 0){
        _isAdd = true;
      }else{
        _isAdd = false;
      }
      // console.log(77,_current,_period, date_type, start_date,end_date, _time,_diff);
      this.triggerEvent('change', {
        period: _period,
        date_type: date_type,
        start_date: start_date,
        time: _time,
      })
      this.setData({
        show: false,
        time: _time,
        currentDate: start_date,
        isAdd: _isAdd
      })
    },
    onClose() {
      this.triggerEvent('map', {
        visible: true
      })
      this.setData({
        show: false
      })
    },
  }
})